/**
 * Copyright (C), 2019-2022, 宇信融汇
 * FileName: Publisher
 * Author:   666
 * Date:     2022/4/21 8:06
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 666           修改时间           版本号              描述
 */
package com.mashibing.pubsub;

import com.mashibing.util.RabbitMQConnectionUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.junit.Test;

/**
 * @Classname Publisher
 * @Description TODO
 * @Date 2022/4/21 8:06
 * @Created by 666
 */
public class Publisher {
    public static final String EXCHANGE_NAME = "pubsub";
    public static final String QUEUE_NAME1 = "pubsub-one";
    public static final String QUEUE_NAME2 = "pubsub-two";

    @Test
    public void publisher() throws Exception {
//        1、创建链接
        Connection connection = RabbitMQConnectionUtil.getConnection();
//        2、创建channel
        Channel channel = connection.createChannel();
//        3、创建exchange  FANOUT绑定方式为直接绑定
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);
//        4、构建queue
        channel.queueDeclare(QUEUE_NAME1, false, false,false, null);
        channel.queueDeclare(QUEUE_NAME2, false, false,false, null);
//        5、交换机绑定
        channel.queueBind(QUEUE_NAME1, EXCHANGE_NAME, "");
        channel.queueBind(QUEUE_NAME2, EXCHANGE_NAME, "");
//        6、发送消息
        channel.basicPublish(EXCHANGE_NAME,"",null,"pubsub/pubslisher".getBytes());
        System.out.println("生产者消息发送");
    }
}
